package org.hhchat.others.wangyi;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class wangyi01 {
    static String RegexDairy = "<diary>|</diary>";
    static String RegexColor = "<color=(.*?)>|</color>";

    public static void main(String[] args){
        int n;
        Scanner scanner = new Scanner(System.in);
        while(scanner.hasNext()){


            n = scanner.nextInt();
            String[] input = new String[n];
            scanner.nextLine();
            for(int i=0;i<n;i++) {
                input[i] = scanner.nextLine();
            }
            Pattern patternRegexDairy = Pattern.compile(RegexDairy);
            Pattern patternRegexColor = Pattern.compile(RegexColor);
            for (int i = 0; i < n; i++) {
                String str = input[i];
                Stack<String> colorstack = new Stack<>();
                colorstack.push("black");
                Map<String, colorcount> countResult = new HashMap<>();
                countResult.put("black", new colorcount("black", 0));
                Matcher RegexColor = patternRegexColor.matcher(str);
                Matcher RegexDiary = patternRegexDairy.matcher(str);
                RegexDiary.find();
                int cur = RegexDiary.end();
                RegexDiary.find();
                int end = RegexDiary.start();
                int last = cur;
                while (RegexColor.find()) {
                    String words = RegexColor.group();
                    String lastcolor;
                    if (words.equals("</color>")) {
                        cur = RegexColor.start();
                        lastcolor = colorstack.peek();
                        colorstack.pop();
                    } else {
                        cur = RegexColor.start();
                        String color = RegexColor.group(1);
                        lastcolor = colorstack.peek();
                        colorstack.push(color);
                    }

                    colorcount lastColorCount = countResult.get(lastcolor);
                    if (lastColorCount != null) {
                        lastColorCount.add(str.substring(last, cur));
                    } else {
                        lastColorCount = new colorcount(lastcolor, 0);
                        lastColorCount.add(str.substring(last, cur));
                        countResult.put(lastcolor, lastColorCount);
                    }
                    last = RegexColor.end();
                }
                if (cur < end) {
                    colorcount lastColorCount = countResult.get("black");
                    lastColorCount.add(str.substring(last,end));
                }

                List<colorcount> result = new ArrayList<>();
                for (Map.Entry<String, colorcount> entry : countResult.entrySet()) {
                    result.add(entry.getValue());
                }

                Collections.sort(result, new Comparator<colorcount>() {
                    @Override
                    public int compare(colorcount o1, colorcount o2) {
                        return o1.getCount()<o2.getCount()?1:o1.getColor().compareTo(o2.getColor());
                    }
                });
                System.out.println("Case " + (i+1));
                for (colorcount c : result) {
                    if(c.getCount()>0)
                        System.out.println(c.getColor() + ": " + c.getCount());
                }
                System.out.println();
            }
        }
    }

    static class colorcount{
        String color;
        int count;

        public void add(String str) {
            count += str.replace(" ", "").length();
        }

        public colorcount(String color, int count) {
            this.color = color;
            this.count = count;
        }

        public String getColor() {
            return color;
        }

        public void setColor(String color) {
            this.color = color;
        }

        public int getCount() {
            return count;
        }

        public void setCount(int count) {
            this.count = count;
        }
    }

}
